/*
 * COPYRIGHT. ShenZhen GreatVision Network Technology Co., Ltd. 2015.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen GreatVision Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * 2015年4月30日    yangzs         Create the class
 */

package com.nj.core.base.plugin;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nj.core.base.util.Tools;


/**
 * @FileName MybatisSqlPlugin.java
 * @Description:
 *
 * @Date 2015年4月30日
 * @author yangzs
 * @version 1.0
 * 
 */
@Intercepts({
		@Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class,
				ResultHandler.class }) })
public class MybatisSqlPlugin implements Interceptor {
	
	private static Logger logger = LoggerFactory.getLogger(MybatisSqlPlugin.class);
	
	//打印地址
	public static final String PRINT_LOG = "log";			//采用第三方日志打印
	public static final String PRINT_CONSOLE = "console";	//打印到控制台

	private boolean showSql = true;
	private String printAddress = PRINT_LOG;		//可以设置多个打印地址，之间以逗号分割

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		Object returnValue = "";
		if (showSql) {
			MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
			Object parameter = null;
			if (invocation.getArgs().length > 1) {
				parameter = invocation.getArgs()[1];
			}
			String sqlId = mappedStatement.getId();
			BoundSql boundSql = mappedStatement.getBoundSql(parameter);
			Configuration configuration = mappedStatement.getConfiguration();
			long start = System.currentTimeMillis();
			returnValue = invocation.proceed();
			long end = System.currentTimeMillis();
			long time = (end - start);
			if (time > 1) {
				String sql = getSql(configuration, boundSql, sqlId, time);
				if(printAddress.contains(PRINT_LOG)){
					logger.info(sql);
				}
				if(printAddress.contains(PRINT_CONSOLE)){
					System.out.println(sql);
				}
			}
		} else {
			returnValue = invocation.proceed();
		}
		return returnValue;
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
		String _showSql = properties.getProperty("showSql");
		if (!Tools.isEmpty(_showSql)) {
			try {
				showSql = Boolean.parseBoolean(_showSql);
			} catch (Exception e) {
				logger.error("showSql property is not legal!");
			}
		}
		String _printAddress = properties.getProperty("printAddress");
		if (!Tools.isEmpty(_printAddress)) {
			if(!_printAddress.equals(PRINT_LOG) && !_printAddress.equals(PRINT_CONSOLE)){
				logger.error("printAddress property is not legal!");
			}else{
				printAddress = _printAddress;
			}
		}
	}

	public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) {
		String sql = showSql(configuration, boundSql);
		StringBuilder str = new StringBuilder(100);
		str.append(sqlId);
		str.append(" : ");
		str.append(sql);
		str.append(" : ");
		str.append(time);
		str.append("ms");
		return str.toString();
	}

	private static String getParameterValue(Object obj) {
		String value = null;
		if (obj instanceof String) {
			value = "'" + obj.toString() + "'";
		} else if (obj instanceof Date) {
			DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
			if(obj==null){
				value = "'" + formatter.format(new Date()) + "'";
			}else{
				value = "'" + formatter.format(obj) + "'";
			}
		} else {
			if (obj != null) {
				value = obj.toString();
			} else {
				value = "";
			}

		}
		return value;
	}

	public static String showSql(Configuration configuration, BoundSql boundSql) {
		/*
		 * ['\\s] 空白字符
		 * java replace 方法直接替换?会报错，需要替换[?]
		 */
		Object parameterObject = boundSql.getParameterObject();
		List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
		String sql = boundSql.getSql().replaceAll("['\\s]+", " ");
		if (parameterMappings.size() > 0 && parameterObject != null) {
			TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
			if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
				sql = sql.replaceFirst("[?]", getParameterValue(parameterObject));

			} else {
				MetaObject metaObject = configuration.newMetaObject(parameterObject);
				for (ParameterMapping parameterMapping : parameterMappings) {
					String propertyName = parameterMapping.getProperty();
					if (metaObject.hasGetter(propertyName)) {
						Object obj = metaObject.getValue(propertyName);
						sql = sql.replaceFirst("[?]", getParameterValue(obj));
					} else if (boundSql.hasAdditionalParameter(propertyName)) {
						Object obj = boundSql.getAdditionalParameter(propertyName);
						sql = sql.replaceFirst("[?]", getParameterValue(obj));
					}
				}
			}
		}
		logger.debug("sql " + sql);
		return sql;
	}

	public boolean isShowSql() {
		return showSql;
	}
}
